
exec(open("import_qt.py").read())
from threading import Timer

import json


class JsHandler(QObject):
  def __init__(self, frame, classCt):
    super(JsHandler, self).__init__()
    self.frame = frame
    self.classCt = classCt
#    r.startRecord(-1, self.logMsg, self);
#    t = Timer(10.0, self.stopRecord)
#    t.start()

  def loaded(self, ok):
    #initial actions with 
    self.frame.addToJavaScriptWindowObject("js2Qt", self);
    devs = self.classCt["audioRec"].deviceList();
    self.frame.evaluateJavaScript('showDevices('+json.dumps(devs)+');')
    if(len(devs)>0):
      self.statusChanged(2)

  @Slot(int)
  def startRecord(self, dev=-1):
    self.classCt["audioRec"].startRecord(dev);
  
  @Slot()
  def stopRecord(self):
    self.classCt["audioRec"].stopRecord();

  def logMsg(self, data):
    self.frame.evaluateJavaScript('log('+json.dumps(data)+');')
    
  def statusChanged(self, status):
    self.frame.evaluateJavaScript('recordStatus('+json.dumps(status)+');')
    if(status==0):
      self.classCt["volumeMeter"].start()
    else:
      self.classCt["volumeMeter"].stop()
  
